﻿using System;
using System.Collections.Generic;
using System.Web;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;
using OneMG.Phoenix.Kernel.Util;
using OneMG.Phoenix.Kernel.Enum;

namespace OneMG.Phoenix.AdminSite.Views.Links
{

    public class Save : IHttpHandler
    {
        ILinksService ILinks = new LinksService();

        public void ProcessRequest(HttpContext context)
        {



            int actionType = 0;
            int.TryParse(context.Request.Form["hdaction"], out actionType);
            string action = "";
            switch ((ActionType)actionType)
            {
                case ActionType.Insert:
                    {
                        LinksModel model = new LinksModel();

                        model.LinkName = context.Request.Form["txtLinkName"];
                        model.LinkSort = int.Parse(context.Request.Form["txtSort"]);
                        model.LinkUrl = context.Request.Form["txtLinkUrl"];
                        model.CreateTime = DateTime.Now;
                        model.CreateUser = 1;
                        model.UpdateTime = DateTime.Now;
                        model.UpdateUser = 1;

                        ILinks.AddLinks(model);

                        action = "添加成功";
                    }
                    break;
                case ActionType.Delete:
                    {
                        int linkID = int.Parse(context.Request.Form["LinkID"]);
                        ILinks.DeleteByLinkID(linkID);
                        action = "删除成功";


                    }
                    break;
                case ActionType.Modify:
                    {
                        int linkID = int.Parse(context.Request.Form["hdLinkID"]);
                        LinksModel model = ILinks.GetByLinkID(linkID);
                        model.LinkName = context.Request.Form["txtLinkName"];
                        model.LinkSort = int.Parse(context.Request.Form["txtSort"]);
                        model.LinkUrl = context.Request.Form["txtLinkUrl"];
                        model.UpdateTime = DateTime.Now;
                        ILinks.UpdateByLinkID(model);
                        action = "编辑操作成功";

                    }
                    break;

            }

            JsonResult.Result(context, JsonMessageStatus.OK, action);


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
